package com.ruoyi.syrw.controller;

import java.util.List;
import java.util.Arrays;

import cn.dev33.satoken.annotation.SaIgnore;
import com.ruoyi.report_tg.domain.vo.ExcelGeneralVo;
import com.ruoyi.syrw.domain.bo.ModPagesBo;
import com.ruoyi.syrw.domain.bo.ReportBo;
import com.ruoyi.syrw.domain.vo.QrCodeVo;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;

import cn.dev33.satoken.annotation.SaCheckLogin;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.syrw.domain.vo.BsDataReportVo;
import com.ruoyi.syrw.domain.bo.BsDataReportBo;
import com.ruoyi.syrw.service.IBsDataReportService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 数据报告
 *
 * @author zyb
 * @date 2024-04-30
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/syrw/bsDataReport")
public class BsDataReportController extends BaseController {

    private final IBsDataReportService iBsDataReportService;

    /**
     * 查询数据报告列表
     */
    @SaCheckLogin
    @GetMapping("/list")
    public TableDataInfo<BsDataReportVo> list(BsDataReportBo bo, PageQuery pageQuery) {
        return iBsDataReportService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出数据报告列表
     */
    @SaCheckLogin
    @Log(title = "数据报告", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BsDataReportBo bo, HttpServletResponse response) {
        List<BsDataReportVo> list = iBsDataReportService.queryList(bo);
        ExcelUtil.exportExcel(list, "数据报告", BsDataReportVo.class, response);
    }

    /**
     * 获取数据报告详细信息
     *
     * @param reportId 主键
     */
    @SaCheckLogin
    @GetMapping("/{reportId}")
    public R<BsDataReportVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long reportId) {
        return R.ok(iBsDataReportService.queryById(reportId));
    }

    /**
     * 新增数据报告
     */
    @SaCheckLogin
    @Log(title = "数据报告", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BsDataReportBo bo) {
        return toAjax(iBsDataReportService.insertByBo(bo));
    }

    /**
     * 修改数据报告
     */
    @SaCheckLogin
    @Log(title = "数据报告", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BsDataReportBo bo) {
        return toAjax(iBsDataReportService.updateByBo(bo));
    }


    /**
     * 复制一份数据报告
     */
    @SaCheckLogin
    @Log(title = "数据报告", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PutMapping("/copy")
    public R<Void> copy(@Validated(EditGroup.class) @RequestBody BsDataReportBo bo) {
        return toAjax(iBsDataReportService.copy(bo));
    }

    /**
     * 删除数据报告
     *
     * @param reportIds 主键串
     */
    @SaCheckLogin
    @Log(title = "数据报告", businessType = BusinessType.DELETE)
    @DeleteMapping("/{reportIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] reportIds) {
        return toAjax(iBsDataReportService.deleteWithValidByIds(Arrays.asList(reportIds), true));
    }

    /**
     * 数据报告 填报保存
     */
    @SaCheckLogin
    @Log(title = "数据报告", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/tb")
    public R<Void> tb(@Validated(EditGroup.class) @RequestBody List<ReportBo> bos) {
        return toAjax(iBsDataReportService.tb(bos));
    }


    /**
     * 加减页
     */
    @SaCheckLogin
    @RepeatSubmit()
    @PostMapping("/modPages")
    public R<Void> modPages(@Validated @RequestBody ModPagesBo bo) {
        return toAjax(iBsDataReportService.modPages(bo));
    }

    /**
     * 获取报告二维码源串
     */
    @SaCheckLogin
    @RepeatSubmit()
    @GetMapping("/qrcode/{reportId}")
    public R<QrCodeVo> qrcode(@NotNull(message = "reportId不能为空")
                                     @PathVariable Long reportId) {
        return R.ok(iBsDataReportService.qrcode(reportId));
    }

    /**
     * 获取试验任务的报告内容List
     */
    @SaIgnore
    @PostMapping("/bgInfos")
    public R<List<ExcelGeneralVo>> bgInfos(@RequestBody BsDataReportBo bo) {
        return R.ok(iBsDataReportService.bgInfos(bo.getReportId()));
    }



}
